import {
  hasChild,
  localRead,
  getToken
} from '@/libs/util'
import Main from '@/components/main'
import router from '../router/index.js'

// 加载菜单
export const loadMenu = () => {
  let list = []

  let data = localRead('login_res_nav')

  if (!data) {
    return list
  }

  let filterMemu = JSON.parse(data);
  let filterArr = [];

  list = JSON.parse(JSON.stringify(filterMemu)) //商家版返回的是对象，总后台是数组
 
  list = formatMenu(list)
  return list
}

// 格式化菜单
export const formatMenu = (list) => {


  let res = [];
  // list.forEach(function(item) {
    for(var i in list){
      let item = list[i];
      let obj = {
        path: item.path,
        //  name: item.title,
        name: item.path.substring(1, item.path.length),
      }

      obj.meta = {
        title: item.title,
        icon: item.class
      }
      if (!item.vue_url) {
        obj.component = Main
      } else {
        let data = item.vue_url
        // 这里的data应为 /notice/notice.vue 类似的数据，对应的是src/view/下的本地文件
        obj.component = () => import('@/view/' + data);
      }
      if (hasChild(item)) {
        obj.children = formatMenu(item.sub)

      }
      res.push(obj)
}
  // })
  
  return res
}
